<?php
/**
 * BootAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using BootAdmin.
 *
 * @Author G.r<root@BootAdmin.com>
 */

declare (strict_types=1);

namespace app\admin\validate;

use app\admin\model\Menu;
use app\common\validate\BaseValidate;

/**
 * 菜单验证器
 */
class MenuValidate extends BaseValidate
{
    protected $rule = [
        'id' => 'require|checkMenu',
        'name' => 'require|length:5,32',
        'code' => 'require|length:1,16|unique:'.Menu::class,
        'type' => 'require|in:M,B,L,I',
        'status' => 'require|in:0,1',
        'sort' => 'require|number'
    ];

    protected $message = [
        'id.require' => '菜单id不能为空',
        'name.require' => '菜单名称不能为空',
        'name.length' => '菜单名称长度须在5-32位字符',
        'code.require' => '菜单代码不能为空',
        'code.length' => '菜单代码长度须在1-16位字符',
        'code.unique' => '菜单代码已存在',
        'type.require' => '请选择菜单类型',
        'type.in' => '菜单类型值错误',
        'sort.require' => '排序不能为空',
        'sort.number' => '排序值必须为数字',
        'status.require' => '请选择状态',
        'status.in' => '状态值错误'
    ];

    /**
     * 添加场景
     * @return MenuValidate
     */
    public function sceneAdd()
    {
        return $this->remove('id', 'require|checkMenu');
    }

    /**
     * 详情场景
     * @return MenuValidate
     */
    public function sceneDetail()
    {
        return $this->only(['id']);
    }

    /**
     * 编辑场景
     * @return MenuValidate
     */
    public function sceneEdit()
    {
        return $this->append('id', 'require|checkMenu');
    }


    /**
     * 删除场景
     * @return MenuValidate
     */
    public function sceneDelete()
    {
        return $this->only(['id']);
    }

    /**
     * 恢复场景
     * @return MenuValidate
     */
    public function sceneRecovery()
    {
        return $this->only(['id']);
    }

    /**
     * 检查指定菜单是否存在
     * @param $value
     * @return bool|string
     */
    public function checkMenu($value)
    {
        $dept = Menu::whereIn('id', $value)->withTrashed()->select();
        if ($dept->isEmpty()) {
            return '菜单不存在';
        }
        return true;
    }
}